using UnityEngine;
using System.Collections;

/**
 * Script of time managing behaviour description.
 * The purpose of this class is to calculate the current timer.
 * @author Benjamin Bruneau
 */
public class TimeScript : MonoBehaviour 
{
	private const string REPLAY_MODE_NAME = "ReplayMode"; // Replay Mode GameObject's name
	
	private float _referenceTime; // Start time
	private float _ellapsedTime; // Ellapsed time
	private float _timeAlterer = 0f; // Bonus (subtracted time)
	
	private bool _win = false; // Win
	
	public bool _isPlaying = true; // Is the game in action
	public bool _isReplayMode; // Is replay
	
	public GameObject _replayMode; // Replay mode object
	
	
	
	/**
	 * Called on initialization.
	 * @return void
	 */
	void Start () 
	{
		// Persist Game Object after scene
		DontDestroyOnLoad(this);
		
		//Initialize Start time
		_referenceTime = Time.time;
		
		//Initialized because this object moves through scenes
		_replayMode = GameObject.Find(REPLAY_MODE_NAME);
		
		if(_replayMode.GetComponent<ReplayModeScript>()._replayMode)
		{
			_isReplayMode = true;
		}
	}
	
	/**
	 * Called once per frame.
	 * @return void
	 */
	void Update () 
	{
		// Update timer
		if (_isPlaying) 
		{
		_ellapsedTime = Time.time - _referenceTime - _timeAlterer;
		}
	}
	
	/**
	 * Time getter.
	 * @return float
	 */
	public float getTime()
	{
		return _ellapsedTime;
	}
	
	/**
	 * Adds time bonus.
	 * @param float
	 * @return void
	 */
	public void alterTime(float pAlterer)
	{
		// Bonus has to be smaller than the ellapsed time
		if (_ellapsedTime > pAlterer)
		{
			_timeAlterer += pAlterer;
		} else {
			_timeAlterer += _ellapsedTime;
		}	
	}
	
	/**
	 * Win status getter.
	 * @return bool
	 */
	public bool getWin()
	{
		return _win;
	}
	
	/**
	 * Win to true (game won)
	 * @return void
	 */
	public void gameWon()
	{
			_win = true;
	}
}
